package com.bw.apps.net

import android.content.Context
import android.os.Environment
import android.view.LayoutInflater
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.apps.databinding.ActivityNetAppsBinding
import com.bw.common.mvi.BaseActivity
import com.bw.network.download.DownloadStatus
import com.bw.quickadapter.BaseQuickMultipleAdapter
import com.bw.widget.dialog.DownLoadDialog
import com.lody.virtual.client.core.VirtualCore
import io.virtualapp.hook_sdk.app.InstallAppUtils
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.io.File

@Route(path = "/apps/NetAppsActivity")
class NetAppsActivity : BaseActivity<ActivityNetAppsBinding,NetAppsViewModel>(){

    private val adapter : NetAppsAdapter by lazy { NetAppsAdapter() }
    private val installAppUtils : InstallAppUtils by lazy {InstallAppUtils() }
    private var dialog : DownLoadDialog ?=null
    private lateinit var ctx : Context
    private val apkPath : String by lazy {
        getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!.absolutePath
    }
    private lateinit var path:String
    override fun createVM(): NetAppsViewModel {
        return viewModels<NetAppsViewModel>().value
    }

    override fun initView() {
        ctx = this
        v.netAppsBar.backImg.setOnClickListener { finish() }
        v.netAppsBar.titleTv.text = "应用市场"
        val manager = GridLayoutManager(this,3)
        manager.orientation = GridLayoutManager.VERTICAL
        v.netAppsRv.layoutManager = manager
        adapter.setOnItemCLickListener { adapter, position ->
            val netAppsAdapter : NetAppsAdapter = adapter as NetAppsAdapter
            path = apkPath+"/"+netAppsAdapter.data.get(position).appName+".apk"
            vm.downloadApp(netAppsAdapter.data.get(position).apkUrl,path)
        }
        v.netAppsRv.adapter = adapter
    }

    override fun initData() {
        vm.selApps(1)
    }

    override fun bindState() {
        lifecycleScope.launch {
            vm.mutableContainer.uiStateFlow.collect{
                if (it.appsState) {
                    adapter.setNewInstance(it.apps)
                }
                if (it.startState){
                    dialog = DownLoadDialog(ctx)
                    dialog!!.let {
                        it.maxPrg = 100
                        it.titleStr = "测试下载"
                        it.show() }
                }
                if (it.doneState){
                    dialog!!.dismiss()
                    if(!File(apkPath).exists()){
                        ToastUtils.showShort("下载失败")
                        return@collect
                    }
                    //安装app
                    val flag = installAppUtils.installApp(path)
                    LogUtils.e("安装结果"+flag)
                    if (flag) {
                        ToastUtils.showShort("安装成功")
                    }
                    else
                        ToastUtils.showShort("安装失败")
                }
                if (it.downLoadState){
                    val progress : DownloadStatus.Progress = it.downLoad as DownloadStatus.Progress
                    dialog!!.setPrgNum(progress.progress)
                }
            }
        }
    }

    override fun createViewBinding(): ViewBinding {
        return ActivityNetAppsBinding.inflate(LayoutInflater.from(this))
    }
}